package com.k2data.k2app.controller;

import com.k2data.k2app.domain.SysAmq;
import com.k2data.k2app.mapper.SysAmqMapper;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.service.SysAmqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 短信网管配
 * Created by clb on 17-3-17.
 */
@Api(description = "短信网管配置")
@RestController
@RequestMapping("/amq")
public class SysAmqController {
	private final SysAmqMapper sysAmqMapper;
	private final SysAmqService sysAmqService;

	@Autowired
	public SysAmqController (SysAmqMapper sysAmqMapper, SysAmqService sysAmqService) {
		this.sysAmqMapper = sysAmqMapper;
		this.sysAmqService = sysAmqService;
	}

	@ApiOperation(value = "创建")
	@ApiImplicitParam(name = "sysAmq", value = "sysAmq对象", required = true, dataType = "SysAmq", paramType = "body")
	@PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<CommonResultResponse> post(@RequestBody SysAmq sysSms) throws Exception {
		return ResponseHelper.success(sysAmqService.insert(sysSms));
	}

	@ApiOperation(value = "删除")
	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
	@DeleteMapping(value = "/{id}")
	public ResponseEntity<CommonResultResponse> delete(@PathVariable("id") String id) throws Exception {
		return ResponseHelper.success(sysAmqMapper.deleteById(Long.parseLong(id)));
	}

	@ApiOperation(value = "更新")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path"),
			@ApiImplicitParam(name = "sysAmq", value = "sysAmq对象", required = true, dataType = "SysAmq", paramType = "body")
	})
	@PutMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<CommonResultResponse> update(@PathVariable("id") Long id,
													   @RequestBody SysAmq sysAmq) throws Exception {
		sysAmq.setId(id);
		return ResponseHelper.success(sysAmqService.update(sysAmq));
	}

	@ApiOperation(value = "根据id获取")
	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path")
	@GetMapping(value = "/{id}")
	public ResponseEntity<CommonResultResponse> get(@PathVariable("id") Long id) throws Exception {
		return ResponseHelper.success(sysAmqService.get(id));
	}

	@ApiOperation(value = "获取所有记录")
	@GetMapping(value = "")
	public ResponseEntity<CommonResultResponse> selectAll() throws Exception {
		return ResponseHelper.success(sysAmqMapper.selectAll());
	}

	@ApiOperation(value = "获取有效记录")
	@GetMapping(value = "/useful")
	public ResponseEntity<CommonResultResponse> selectUseful() throws Exception {
		List<SysAmq> list = sysAmqMapper.selectUseful();
		if(list.size()>0){
			return ResponseHelper.success(list.get(0));
		}else{
			return ResponseHelper.success(new SysAmq());
		}
	}
}
